<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Products extends MX_Controller {

	private $module = 'products';
	private $table = 'products';
	function __construct(){
		parent::__construct();
		
		$this->load->model($this->module.'_model','model');
		$this->load->model('admincp_modules/admincp_modules_model');
		$this->load->model('category/category_model','category');
		if($this->uri->segment(1)=='admincp'){
			if($this->uri->segment(2)!='login'){
				if(!$this->session->userdata('userInfo')){
					header('Location: '.PATH_URL.'admincp/login');
					return false;
				}
				$get_module = $this->admincp_modules_model->check_modules($this->uri->segment(2));
				$this->session->set_userdata('ID_Module',$get_module[0]->id);
				$this->session->set_userdata('Name_Module',$get_module[0]->name);
			}
			$this->template->set_template('admin');
			$this->template->write('title','Admin Control Panel');
		}
	}
	/*------------------------------------ Admin Control Panel ------------------------------------*/
	public function admincp_index(){
		modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'r',0);
		$default_func = 'created';
		$default_sort = 'DESC';
		$data = array(
			'module'=>$this->module,
			'module_name'=>$this->session->userdata('Name_Module'),
			'default_func'=>$default_func,
			'default_sort'=>$default_sort
		);
		$this->template->write_view('content','BACKEND/index',$data);
		$this->template->render();
	}
	
	
	public function admincp_update($id=0){
		$this->load->model('Products_model');
		if($id==0){
			modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',0);
		}else{
			modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'r',0);
		}
		$result[0] = array();
		if($id!=0){
			$result = $this->model->getDetailManagement($id);
			$dataUnread = array(
				'status'=>1
			);
			modules::run('admincp/saveLog',$this->module,$id,'status','update',0,1);
			$this->db->where('id', $id);
			$this->db->update($this->table, $dataUnread);
		}
		$cate=$this->category->getCategory();
		$data = array(
			'result'=>$result[0],
			'module'=>$this->module,
			'id'=>$id,
			'cate'=>$cate,
		);
		$this->template->write_view('content','BACKEND/ajax_editContent',$data);
		$this->template->render();
	}
	
	public function admincp_save(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',1);
		if($_POST){
			//Upload Image
			$fileName = array('image'=>'');
			if($_FILES){
				foreach($fileName as $k=>$v){
					if(isset($_FILES['fileAdmincp']['error'][$k])){
						$typeFileImage = strtolower(substr($_FILES['fileAdmincp']['type'][$k],0,5));
						if($typeFileImage == 'image'){
							$tmp_name[$k] = $_FILES['fileAdmincp']["tmp_name"][$k];
							$file_name[$k] = $_FILES['fileAdmincp']['name'][$k];
							$ext = strtolower(substr($file_name[$k], -4, 4));
							if($ext=='jpeg'){
								$fileName[$k] = time().'_'.SEO(substr($file_name[$k],0,-5)).'.jpg';
							}else{
								$fileName[$k] = time().'_'.SEO(substr($file_name[$k],0,-4)).$ext;
							}
						}else{
							print 'Only upload Image.';
							exit;
						}
					}
				}
			}
			//End Upload Image
			
			if($this->model->saveManagement($fileName)){
				//Upload Image
				if($_FILES){
					foreach($fileName as $k=>$v){
						if(isset($_FILES['fileAdmincp']['error'][$k])){
							$upload_path = BASEFOLDER.IMAGES_PRODUCTS.$this->input->post('folderAdmincp').'/';
							move_uploaded_file($tmp_name[$k], $upload_path.$fileName[$k]);
						}
					}
				}
				//End Upload Image
				print 'success';
				exit;
			}
		}
	}
	
	public function admincp_delete(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'d',1);
		if($perm=='permission-denied'){
			print $perm;
			exit;
		}
		if($this->input->post('id')){
			$id = $this->input->post('id');
			$result = $this->model->getDetailManagement($id);
			$folderTmp=explode('/',$result[0]->image);
			modules::run('admincp/saveLog',$this->module,$id,'Delete','Delete');
			$this->db->where('id',$id);
			if($this->db->delete($this->table)){
				//Xóa hình khi Delete
				@unlink(BASEFOLDER.IMAGES_PRODUCTS.$folderTmp[0].'/'.$folderTmp[1]);
				return true;
			}
		}
	}
	
	public function admincp_ajaxLoadContent(){
		$this->load->library('AdminPagination');
		$config['total_rows'] = $this->model->getTotalsearchContent();
		$config['per_page'] = $this->input->post('per_page');
		$config['num_links'] = 3;
		$config['func_ajax'] = 'searchContent';
		$config['start'] = $this->input->post('start');
		$this->adminpagination->initialize($config);

		$result = $this->model->getsearchContent($config['per_page'],$this->input->post('start'));
		$data = array(
			'result'=>$result,
			'per_page'=>$this->input->post('per_page'),
			'start'=>$this->input->post('start'),
			'module'=>$this->module
		);
		$this->session->set_userdata('start',$this->input->post('start'));
		$this->load->view('BACKEND/ajax_loadContent',$data);
	}
	
	public function admincp_ajaxSelect(){
		$idCate=$this->input->post('idCate');
		$folder=$this->category->getFolder($idCate);
		$showTags='';
		foreach($folder as $r){
			$showTags .='<option value="'.$r->folder.'">'.$r->folder.'</option>';
		}
		print $showTags;
	}
	
	public function admincp_ajaxUpdateStatus(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',1);
		if($perm=='permission-denied'){
			print '<script type="text/javascript">show_perm_denied()</script>';
			$status = $this->input->post('status');
			$data = array(
				'status'=>$status
			);
		}else{
			if($this->input->post('status')==0){
				$status = 1;
			}else{
				$status = 0;
			}
			$data = array(
				'status'=>$status
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('id'),'status','update',$this->input->post('status'),$status);
			$this->db->where('id', $this->input->post('id'));
			$this->db->update($this->table, $data);
		}
		
		$update = array(
			'status'=>$status,
			'id'=>$this->input->post('id'),
			'module'=>$this->module
		);
		$this->load->view('BACKEND/ajax_updateStatus',$update);
	}
	
	public function admincp_ajaxUpdateNews(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',1);
		if($perm=='permission-denied'){
			print '<script type="text/javascript">show_perm_denied()</script>';
			$news = $this->input->post('news');
			$data = array(
				'news'=>$news
			);
		}else{
			if($this->input->post('news')==0){
				$news = 1;
			}else{
				$news = 0;
			}
			$data = array(
				'news'=>$news
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('id'),'news','update',$this->input->post('news'),$news);
			$this->db->where('id', $this->input->post('id'));
			$this->db->update($this->table, $data);
		}
		
		$update = array(
			'news'=>$news,
			'id'=>$this->input->post('id'),
			'module'=>$this->module
		);
		$this->load->view('BACKEND/ajax_updateNews',$update);
	}
	
	public function admincp_ajaxUpdateSellers(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',1);
		if($perm=='permission-denied'){
			print '<script type="text/javascript">show_perm_denied()</script>';
			$sellers = $this->input->post('sellers');
			$data = array(
				'sellers'=>$sellers
			);
		}else{
			if($this->input->post('sellers')==0){
				$sellers = 1;
			}else{
				$sellers = 0;
			}
			$data = array(
				'sellers'=>$sellers
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('id'),'sellers','update',$this->input->post('sellers'),$sellers);
			$this->db->where('id', $this->input->post('id'));
			$this->db->update($this->table, $data);
		}
		
		$update = array(
			'sellers'=>$sellers,
			'id'=>$this->input->post('id'),
			'module'=>$this->module
		);
		$this->load->view('BACKEND/ajax_updateSellers',$update);
	}
	
	public function admincp_ajaxUpdatePromotion(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',1);
		if($perm=='permission-denied'){
			print '<script type="text/javascript">show_perm_denied()</script>';
			$promotion = $this->input->post('promotion');
			$data = array(
				'promotion'=>$promotion
			);
		}else{
			if($this->input->post('promotion')==0){
				$promotion = 1;
			}else{
				$promotion = 0;
			}
			$data = array(
				'promotion'=>$promotion
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('id'),'promotion','update',$this->input->post('promotion'),$promotion);
			$this->db->where('id', $this->input->post('id'));
			$this->db->update($this->table, $data);
		}
		
		$update = array(
			'promotion'=>$promotion,
			'id'=>$this->input->post('id'),
			'module'=>$this->module
		);
		$this->load->view('BACKEND/ajax_updatePromotion',$update);
	}
	/*------------------------------------ End Admin Control Panel --------------------------------*/
	
	
	/*------------------------------------ FRONTEND ------------------------------------*/
	
	public function index($id=''){	
		$this->load->library('AdminPagination');
					
		$config['total_rows'] = $this->model->getTotal();
		$config['per_page'] = 9;
		$config['num_links'] = 3;
		$config['func_ajax'] = 'searchContent';
		$config['start'] = 0;
		$this->adminpagination->initialize($config);

		$result = $this->model->getsearch($config['per_page'],$config['start']);
		
		$this->template->write('title','Sản phẩm');
		$data=array(
			'result' =>$result,
			'module' => $this->module,
			'start'=>$this->input->post('start'),
		);
		$this->session->set_userdata('start',$this->input->post('start'));
		$this->template->write_view('content','FRONTEND/index',$data);	
		$this->template->render();
	}
	
	public function category($id){
		$this->load->library('AdminPagination');		
		
		$config['total_rows'] = $this->model->getTotal_getProByCate($id);
		$config['per_page'] = 9;
		$config['num_links'] = 3;
		$config['func_ajax'] = 'searchContent';
		$config['start'] = 0;
		$this->adminpagination->initialize($config);

		$result = $this->model->getsearch_getProByCate($id,$config['per_page'],$config['start']);
		$cate=$this->category->getTitle($id);
		
		$this->template->write('title',$cate[0]->title);
		$data=array(
			'idCate' => $id,
			'result' =>$result,
			'module' => $this->module,
			'start'=>$this->input->post('start'),
			'titleCate' => $cate[0]->title,
		);
		$this->session->set_userdata('start',$this->input->post('start'));
		$this->template->write_view('content','FRONTEND/category',$data);	
		$this->template->render();
	}
	
	public function ajaxLoad(){
		$show_category=$this->input->post('show_category');
		$this->load->library('AdminPagination');
		if($show_category){
			$config['total_rows'] = $this->model->getTotal_getProByCate($this->input->post('id_category'));
		}else{
			$config['total_rows'] = $this->model->getTotal();
		}
		$config['per_page'] = $this->input->post('per_page');
		$config['num_links'] = 3;
		$config['func_ajax'] = 'searchContent';
		$config['start'] = $this->input->post('start');
		$this->adminpagination->initialize($config);
		
		if($show_category){
			$result = $this->model->getsearch_getProByCate($this->input->post('id_category'),$config['per_page'],$this->input->post('start'));
		}else{
			$result = $this->model->getsearch($config['per_page'],$this->input->post('start'));
		}
		$data = array(
			'result'=>$result,
			'start'=>$this->input->post('start'),
			'module'=>$this->module
		);
		$this->session->set_userdata('start',$this->input->post('start'));
		$this->load->view('FRONTEND/ajaxload',$data);	
	}
	
	public function detail($id){
		
				
		$result=$this->model->getOne($id);
		$this->template->write('title',$result[0]->title);
		$nameCate=$this->category->getTitle($result[0]->idCategory);
		$data=array(
			'result' =>$result[0],
			'module' => $this->module,
			'nameCate' => $nameCate,
		);
		$this->template->write_view('content','FRONTEND/detail',$data);	
		$this->template->render();
	}
	
	public function searchProducts(){
		$this->load->library('AdminPagination');
		
		$config['total_rows'] = $this->model->getsearchTotalPro($this->input->post('keySearch'));
		$config['per_page'] = 9;
		$config['num_links'] = 3;
		$config['func_ajax'] = 'searchProducts';
		$config['start'] = 0;
		$this->adminpagination->initialize($config);

		$result = $this->model->getsearchProduct($this->input->post('keySearch'),$config['per_page'],$config['start']);
		
		$this->template->write('title','Sản phẩm');
		$data=array(
			'result' =>$result,
			'module' => $this->module,
			'start'=>$this->input->post('start'),
			'a' => $this->input->post('keySearch'),
		);
		
		$this->session->set_userdata('start',$this->input->post('start'));
		$this->template->write_view('content','FRONTEND/search_products',$data);	
		$this->template->render();
	}
	
	public function searchProductsAjax(){
		$this->load->library('AdminPagination');
		
		$config['total_rows'] = $this->model->getsearchTotalPro($this->input->post('keySearch'));
		$config['per_page'] = $this->input->post('per_page');
		$config['num_links'] = 3;
		$config['func_ajax'] = 'searchProducts';
		$config['start'] = $this->input->post('start');
		$this->adminpagination->initialize($config);

		$result = $this->model->getsearchProduct($this->input->post('keySearch'),$config['per_page'],$config['start']);
		
		$this->template->write('title','Sản phẩm');
		$data=array(
			'result' =>$result,
			'module' => $this->module,
			'start'=>$this->input->post('start'),
		);
		
		$this->session->set_userdata('start',$this->input->post('start'));
		$this->load->view('FRONTEND/search_products_ajax',$data);
	}
	
	/*------------------------------------ End FRONTEND --------------------------------*/ 
}